package com.gutai.admin.biz.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * 
 * 请求认证拦截器
 * 
 * <pre>
 * HISTORY
 * ****************************************************************************
 *  ID    DATE            PERSON            REASON
 *  1     2019年1月25日         la.01367604           Create
 * ****************************************************************************
 * </pre>
 * 
 * @author la.01367604
 * @version 1.0
 */
@Component
public class ReqAuthInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(ReqAuthInterceptor.class);

    /**
     * 进入controller层之前拦截请求
     * 
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        // logger.info("开始进入请求地址拦截----------------------------");
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");  
        httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");  
        httpServletResponse.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
        httpServletResponse.setHeader("X-Powered-By","Jetty");  
  
        if (httpServletRequest.getMethod().equals("OPTIONS")) {
            httpServletResponse.setStatus(200);  
            return false;  
        }
        return true;  
    }

//    @Override
//    public void postHandle(HttpServletRequest httpServletRequest,
//            HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView)
//            throws Exception {
//        logger.info("处理请求完成后视图渲染之前的处理操作---------------");
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest httpServletRequest,
//            HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        logger.info("---------------视图渲染之后的操作-------------------------0");
//    }
}
